home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- CUnorderedList.c
-
- CUnorderedList maintains an unordered cluster of objects. It also adds
- methods for iterating over the list.
-
- Copyright (C) 1992 by Brown University. All rights reserved.
-
- Permission is granted to any individual or institution to use, copy,
- or redistribute the binary version of this software and its
- documentation provided this notice and the copyright notices are
- retained. Permission is granted to any individual or non-profit
- institution to use, copy, modify, or redistribute the source files
- of this software provided this notice and the copyright notices are
- retained. This software may not be distributed for profit, either
- in original form or in derivative works, nor can the source be
- distributed to other than an individual or a non-profit institution.
- Any individual or group interested in seeing and/or using these
- source files but who are prevented from doing so by the above
- constraints should contact Don Wolfe, Vice-President for Computer
- Systems at Brown University, (401) 863-7247, for possible
- software licensing of the source developed at Brown.
-
- Brown University and Andrew James Gilmartin make no representations
- about the suitability of this software for any purpose.
-
- BROWN UNIVERSITY AND ANDREW JAMES GILMARTIN GIVE NO WARRANTY, EITHER
- EXPRESS OR IMPLIED, FOR THE PROGRAM AND/OR DOCUMENTATION PROVIDED,
- INCLUDING, WITHOUT LIMITATION, WARRANTY OF MERCHANTABILITY AND
- WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE.
-
- AUTHOR: Andrew_Gilmartin@Brown.Edu
- MODIFIED: 93-04-12
-
- ******************************************************************************/
-
- #include "CUnorderedList.h"
-
-
- /*=============================================================================
- DefaultCompare
-
- This is the default ordering function for the list. It orders the content
- based on memory address (not too useful and ordering).
- =============================================================================*/
-
- static int DefaultCompare( void* a, void* b )
- {
- return (long) a - (long) b;
-
- } /* DefaultCompare */
-
-
-
- /******************************************************************************
- IUnorderedList
-
- Initialize the list. Note that if no compare function is specified then
- use the defualt.
- ******************************************************************************/
-
- void CUnorderedList::IUnorderedList( CompareFunc compare )
- {
- ICluster();
-
- SetCompare( compare );
-
- } /* IUnorderedList */
-
-
-
- /******************************************************************************
- SetCompare
-
- Change the function used to order and compare items in the list. If the
- list should be reordered immediatly, then pass TRUE to reorder. Note, if
- the new compare function does not give the same ordering as the previous
- FindIndex() will not work as expected.
- ******************************************************************************/
-
- void CUnorderedList::SetCompare( CompareFunc compare )
- {
- fCompare = compare != NULL ? compare : (CompareFunc) DefaultCompare;
-
- } /* SetCompare */
-
-
-
- /******************************************************************************
- FindIndex
-
- Find the offset of the object in the items list. This method uses bisection
- to narrow down where the object is in the list. If the object is found then
- return TRUE, otherwise return FALSE.
- ******************************************************************************/
-
- Boolean CUnorderedList::FindIndex( CObject* anObject, long* foundindex )
- {
- long index;
-
- for ( index = 0; index < numItems; index++ )
- {
- long relation = (*fCompare)( anObject, (CObject*) (*items)[ index ] );
-
- if ( relation == 0 )
- {
- *foundindex = index + 1;
- return TRUE;
- }
- }
-
- *foundindex = numItems + 1;
- return FALSE;
-
- } /* FindIndex */
-
-
-
- /******************************************************************************
- AddAll
-
- Add all the objects in a cluster to the list.
- ******************************************************************************/
-
- static void _AddAll( CObject* anObject, CUnorderedList* aList )
- {
- aList->Add( anObject );
- }
-
- void CUnorderedList::AddAll( CCluster* aCluster )
- {
- aCluster->DoForEach1( _AddAll, (long) this );
-
- } /* AddAll */
-
-
-
- /******************************************************************************
- RemoveAll
-
- Remove all the objects from the list.
- ******************************************************************************/
-
- void CUnorderedList::RemoveAll( void )
- {
- while ( numItems > 0 )
- DeleteItem( numItems );
-
- } /* RemoveAll */
-
-
-
- /******************************************************************************
- Includes
-
- Does the list contain the object? Return TRUE if found, FALSE otherwise.
- Modifies fCurrent to point to the found item; use Current() to retrieve
- the object.
- ******************************************************************************/
-
- Boolean CUnorderedList::Includes( CObject* anObject )
- {
- long index;
- Boolean found;
-
- found = FindIndex( anObject, &index );
- if ( found )
- fCurrent = index;
-
- return found;
-
- } /* Includes */
-
-
-
- /******************************************************************************
- At
-
- Return the object at a particular index. Note that it is not an error to
- specify an index outside of the bounds of the list.
- ******************************************************************************/
-
- CObject* CUnorderedList::At( long theIndex )
- {
- if ( 1 <= theIndex && theIndex <= numItems )
- return (CObject*) (*items)[ theIndex - 1 ];
-
- return NULL;
-
- } /* At */
-
-
-
- /******************************************************************************
- First
-
- The methods First, Current, and Next are used to iterate over the content
- of the list. The general form of the code is
-
- for ( list->First(); object = list->Current(); list->Next() )
- ; // Do something
-
- First, Current, and Next use a cursor into the list. First sets the cursor
- to point to the first object in the list; Current is used to get the object
- pointed at by the cursor; Next moves the cursor to the next object in the
- list.
- ******************************************************************************/
-
- void CUnorderedList::First( void )
- {
- fCurrent = 1;
-
- } /* First */
-
-
-
- /******************************************************************************
- Next
-
- Move cursor to the next object in the list.
- ******************************************************************************/
-
- void CUnorderedList::Next( void )
- {
- fCurrent++;
-
- } /* Next */
-
-
-
- /******************************************************************************
- Current
-
- Return the object at the cursor in the list.
- ******************************************************************************/
-
- CObject* CUnorderedList::Current( void )
- {
- return At( fCurrent );
-
- } /* Current */
-
-
-
- /******************************************************************************
- CurrentIndex
-
- Return the index of the current object.
- ******************************************************************************/
-
- long CUnorderedList::CurrentIndex( void )
- {
- return fCurrent;
-
- } /* CurrentIndex */
-
-
-
- /******************************************************************************
- __ForgetCollection
-
- Dispose of the collection and its content. Note, this function is called
- via the macro ForgetCollection(). Macro defined in the header.
- ******************************************************************************/
-
- pascal void __ForgetCluster( Ptr *aClusterAddr )
- {
- CCluster *theCluster = (CCluster*) *aClusterAddr;
-
- if ( theCluster != NULL )
- {
- *aClusterAddr = NULL;
- theCluster->DisposeAll();
- }
-
- } /* __ForgetCluster */
-
-